﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnisysMassiveImporter_v2.Entidades.Configuracion;
using UnisysMassiveImporter_v2.Datos.Archivos;

namespace UnisysMassiveImporter_v2.Entidades.Mensajes
{
    public class InfoMessage:AMessage
    {
        public static int Success_Message = 6;
        public static int Information_Message = 7;


        public InfoMessage(String message, int messageType, int messageCode, String threadName)
        {
            this.message = message;
            this.exception = null;
            this.messageType = messageType;
            this.messageCode = messageCode;
            this.threadName = threadName;
        }
        public InfoMessage() {  }

        public void sendMessage(String message, int messageType, int messageCode, String threadName, ref Configuration appConfig)
        {
            this.message = message;
            this.exception = null;
            this.messageType = messageType;
            this.messageCode = messageCode;
            this.threadName = threadName;
            this.sendMessage(ref appConfig);
        }

        public override String toString()
        {
            return DateTime.Now.ToString("yyyy'-'MM'-'dd HH':'mm':'ss':'fff") + " - " + Logger.ErrorMessage(this.messageType) + " - " + this.message;              
        }

        public override void sendMessage(ref Configuration appConfig)
        {
            if (Logger.ifWrite(this.messageType))
            {
                if (Logger.ifFileWrite())
                {
                    try
                    {
                        Logger.fileWrite(this, ref appConfig);
                    }
                    catch { }
                }

                try
                {
                    using (var proxy = new IServicioTrace.ServicioTraceClient("NetTcpBinding_IServicioTrace"))
                    {
                        var bitacora = new IServicioTrace.TraceGeneral();
                        bitacora.Fecha = DateTime.Now;
                        //bitacora.FechaReal = DateTime.Now;
                        bitacora.CodSistema = appConfig.system;
                      //  bitacora.CodUsuario = "NA";
                        //bitacora.AccionRealizada = "Insertar";
                        bitacora.Detalle = this.toString();
                        proxy.Agregar(bitacora);

                    }
                }
                catch (Exception ex)
                {
                    this.message = "Error al enviar al Web Service de Bitacora el siguiente mensaje(" + ex.Message + "): " + this.message;
                    try
                    {
                        Logger.fileWrite(this, ref appConfig);
                    }
                    catch (Exception e)
                    {
                        Logger.LogThis4Windows("El sistema de bitacoras esta caido, imposible continuar con la aplicacion[mensaje: "+this.message+"]", "Unisys Masive Importer", "Unisys Masive Service");
                        System.Environment.Exit(0);
                    }
                }
            }
        }

       

    }
}
